<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
    <!--  准备好一个容器  -->
    <div id="test"></div>

    <!--  引入react核心库  -->
    <script type="text/javascript" src="../../js_base/react.development.js"></script>
    <script type="text/javascript" src="../../js_base/react-dom.development.js"></script>
    <script type="text/javascript" src="../../js_base/babel.min.js"></script>

    <script type="text/babel">
        class Login extends React.Component{

            // 状态初始化
            state = {
                username: '',
                password: ''
            }

            handleSubmit = (event) => {
                // 阻止默认的表单提交跳转
                event.preventDefault()
                const {username, password} = this.state
                alert(`输入的用户名是：${username}，输入的密码是：${password}`)
            }

            saveUsername = (event) => {
                // 在username输入变化时，将值存储在状态state里
                this.setState({username:event.target.value})
            }

            savePassword = (event) => {
                this.setState({password:event.target.value})
            }

            render(){
                return(
                    <form action="http://www.baidu.com" onSubmit={this.handleSubmit}>
                        用户名：<input onChange={this.saveUsername} type="text" name="username"/>
                        密码：<input onChange={this.savePassword} type="password" name="password"/>
                        <button>登录</button>
                    </form>
                )
            }
        }

        ReactDOM.render(<Login/>, document.getElementById("test"))
    </script>
</body>
</html>